<?php

namespace App\Http\Controllers\Admin;

use App\Model\Code;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Toastr;

class CodeController extends Controller
{
    protected $fields = [
        'batch' => '',
        'length' => 12,
        'prefix' => '',
        'count' => '',
        'is_can' => 0,
    ];

    public function index(Request $request){
        $code = trim($request->code,'');
        $search = array(
            'code' => $code,
        );
        $sql = Code::query();
        $code && $sql->where('code',$code);
        $data = $sql->orderby('status','asc')->paginate(20);
        return view('admin.code.index',['data'=>$data,'search'=>$search]);
    }

    public function add(){
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        $data['batch'] = date('YmdHis');
        return view('admin.code.add',['data'=>$data]);
    }

    public function add_post(Request $request){
        set_time_limit(0);
        $batch = trim($request->input('batch',''));
        $length = (int)$request->input('length',0);
        $prefix = trim($request->input('prefix',''));
        $count = (int)$request->input('count',0);
        $is_can = (int)$request->input('is_can',0);
        for ($i=0; $i<$count; $i++){
            $info = new Code();
            create_guid();
            $info->batch = $batch;
            $code = $prefix.$this->create_guid($length);
            $info->code = $code;
            $info->is_can = $is_can;
            $info->status = 0;
            $info->save();
        }
        Toastr::success('批量添加成功');
        return redirect('/admin/code/index');


    }

    public function create_guid($length) {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid =
            substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12);
        return substr(md5($uuid), 8,$length);
    }

    public function delete($id)
    {
        $tag = Code::find((int)$id);
        if ($tag) {
            $tag->delete();
        } else {
            Toastr::error('删除失败');
            return redirect()->back();
        }
        Toastr::success('删除成功');
        return redirect()->back();
    }

    public function up_down(Request $request){
        $return = array();
        do{
            $id = $request->input('id');
            if(empty($id)){
                $return['code'] = 101;
                $return['message'] = '缺少参数';
                break;
            }
            $info = Code::where('id',$id)->first();
            if(empty($info)){
                $return['code'] = 102;
                $return['message'] = '对象不存在';
                break;
            }
            if($info->status == 1){
                Toastr::error('该激活码已使用');
                $return['code'] = 101;
                $return['message'] = '该激活码已使用';
                break;
            }
            if($info->is_can == 1){
                Toastr::error('该激活码未启用');
                $return['code'] = 101;
                $return['message'] = '该激活码未启用';
                break;
            }
            Toastr::success('操作成功');
            $info->status = 1;
            $info->save();
            $return['code'] = 0;
            $return['message'] = '操作成功';
            break;

        }while (false);
        return response()->json($return);
    }
}
